package com.anjho.bean;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

import com.anjho.dao.DAODistributor;
import com.anjho.dao.DAOGenre;
import com.anjho.dao.DAOProduct;
import com.anjho.dao.DAOPublisher;
import com.anjho.pojo.Distributor;
import com.anjho.pojo.Genre;
import com.anjho.pojo.Product;
import com.anjho.pojo.Publisher;

@ManagedBean(name = "productBean")
@ViewScoped
public class ProductBean extends GenericBean {

	private static final long serialVersionUID = -1211807500652292548L;

	private String barcode;
	private String proNumber;
	private String cover;
	private String name;
	private BigDecimal price;
	private int receivedQtd;
	private Date receiptDate;
	private Date returnDate;

	private int gecode;
	private int pucode;
	private int dicode;

	private Genre genre;
	private Publisher publisher;
	private Distributor distributor;

	private List<Genre> genreList;
	private List<Publisher> publisherList;
	private List<Distributor> distributorList;
	private List<Product> productList;
	private List<Product> filteredProducts;
	private List<Product> productListWhereReturnIsNull;

	private Date currentDate = new Date();

	private Product product;

	public ProductBean() {
		super();
		this.clearForm();
	}

	private void clearForm() {
		this.setBarcode("");
		this.setProNumber("");
		this.setCover("");
		this.setName("");
		this.setPrice(new BigDecimal(0));
		this.setReceivedQtd(0);
		this.setReceiptDate(null);
		this.setReturnDate(null);
		this.genre = null;
		this.publisher = null;
		this.distributor = null;
		this.setProduct(new Product());

		try {
			DAODistributor daoDistributor = new DAODistributor();
			this.distributorList = daoDistributor.getDistributorList();
			daoDistributor = null;

			DAOPublisher daoPublisher = new DAOPublisher();
			this.publisherList = daoPublisher.getPublisherList();
			daoPublisher = null;

			DAOGenre daoGenre = new DAOGenre();
			this.genreList = daoGenre.getGenreList();
			daoGenre = null;
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public String getProNumber() {
		return proNumber;
	}

	public void setProNumber(String proNumber) {
		this.proNumber = proNumber;
	}

	public String getCover() {
		return cover;
	}

	public void setCover(String cover) {
		this.cover = cover;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public int getReceivedQtd() {
		return receivedQtd;
	}

	public void setReceivedQtd(int receivedQtd) {
		this.receivedQtd = receivedQtd;
	}

	public Date getReceiptDate() {
		return receiptDate;
	}

	public void setReceiptDate(Date receiptDate) {
		this.receiptDate = receiptDate;
	}

	public Date getReturnDate() {
		return returnDate;
	}

	public void setReturnDate(Date returnDate) {
		this.returnDate = returnDate;
	}

	public int getGecode() {
		return gecode;
	}

	public void setGecode(int gecode) {
		this.gecode = gecode;
	}

	public int getPucode() {
		return pucode;
	}

	public void setPucode(int pucode) {
		this.pucode = pucode;
	}

	public int getDicode() {
		return dicode;
	}

	public void setDicode(int dicode) {
		this.dicode = dicode;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public List<Genre> getGenreList() {
		return genreList;
	}

	public List<Publisher> getPublisherList() {
		return publisherList;
	}

	public List<Distributor> getDistributorList() {
		return distributorList;
	}

	public List<Product> getProductList() {
		synchronized (this) {
			DAOProduct daoProduct = new DAOProduct();
			try {
				this.productList = daoProduct.getProductList();
			} catch (Exception ex) {
				FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
				FacesContext.getCurrentInstance().addMessage(null, facesMsg);
			}
			return this.productList;
		}
	}
	
	public List<Product> getProductListWhereReturnIsNull() {
		synchronized (this) {
			DAOProduct daoProduct = new DAOProduct();
			try {
				this.productListWhereReturnIsNull = daoProduct.getProductListWhereReturnIsNull();
			} catch (Exception ex) {
				FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
				FacesContext.getCurrentInstance().addMessage(null, facesMsg);
			}
			return this.productListWhereReturnIsNull;
		}
	}

	public Date getCurrentDate() {
		return currentDate;
	}

	public List<Product> getFilteredProducts() {
		return filteredProducts;
	}

	public void setFilteredProducts(List<Product> filteredProducts) {
		this.filteredProducts = filteredProducts;
	}

	public void selDistributorChanged(AjaxBehaviorEvent event) {
		try {
			if (this.getDicode() != 0) {
				this.distributor = new Distributor();
				this.distributor.setDicode(this.getDicode());
				int idxDist = this.getDistributorList().indexOf(
						this.distributor);
				this.distributor = this.getDistributorList().get(idxDist);
			} else {
				this.distributor = new Distributor();
			}
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void selGenreChanged(AjaxBehaviorEvent event) {
		try {
			if (this.getGecode() != 0) {
				this.genre = new Genre();
				this.genre.setGecode(this.getGecode());
				int idxGen = this.getGenreList().indexOf(this.genre);
				this.genre = this.getGenreList().get(idxGen);
			} else {
				this.genre = new Genre();
			}
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void selPublisherChanged(AjaxBehaviorEvent event) {
		try {
			if (this.getPucode() != 0) {
				this.publisher = new Publisher();
				this.publisher.setPucode(this.pucode);
				int idxPuc = this.getPublisherList().indexOf(this.publisher);
				this.publisher = this.getPublisherList().get(idxPuc);
			} else {
				this.publisher = new Publisher();
			}
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void onRegisterClick() throws Exception {
		try {
			Product aux = new Product();

			aux.setBarcode(this.getBarcode());
			aux.setProNumber(this.getProNumber());
			aux.setName(this.getName());
			aux.setCover(this.cover);
			aux.setPrice(this.getPrice());
			aux.setReceivedQtd(this.getReceivedQtd());
			aux.setQtd(this.getReceivedQtd());
			aux.setReceiptDate(this.getReceiptDate());
			aux.setReturnDate(null);

			aux.setDistributor(this.distributor);
			aux.setGenre(this.genre);
			aux.setPublisher(this.publisher);

			DAOProduct daoProduct = new DAOProduct();
			daoProduct.insert(aux);

			this.clearForm();
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}
	
	public void onReturnClick() throws Exception {
		try {
			//this.product.setReturnDate(this.returnDate);
			DAOProduct daoProduct = new DAOProduct();
			daoProduct.update(this.product);
			this.clearForm();
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}
}
